package pe.gob.mef.snip.preinversion.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class Util {
	
	public static Date getDateFromString(String date){
		Date result = null;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		try {
			result =  format.parse(date);
		} catch (ParseException e) {
			result = new Date();
		}
		
		return result;
	}
	
	public static String getStringFromDate(Date date){
		SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
		return format.format(date);
	}
	
	public static String completarCodigoUbigeo(int codigoDepartamento, int codigoProvincia, int codigoDistrito, String tipo){
		try{
			if(tipo.equalsIgnoreCase("DE")){
				return String.format("%2s", Integer.toString(codigoDepartamento)).replace(" ", "0");
			}else if(tipo.equalsIgnoreCase("PR")){
				return String.format("%2s", Integer.toString(codigoDepartamento)).replace(" ", "0")+String.format("%2s", Integer.toString(codigoProvincia)).replace(" ", "0");
			}else if(tipo.equalsIgnoreCase("DI")){
				return String.format("%2s", Integer.toString(codigoDepartamento)).replace(" ", "0")+String.format("%2s", Integer.toString(codigoProvincia)).replace(" ", "0")+String.format("%2s", Integer.toString(codigoDistrito)).replace(" ", "0");
			}else{
				return "-";
			}
		}catch(Exception e){
			return "-";
		}
	}
}
